/*
 * 功能：预约控制层，处理相关http请求
 * 作者：张杨(Sunny)
 * 日期： 2024/11/13 下午4:51
 */
package com.example.springboot.control;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.AuthAccess;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Reserve;
import com.example.springboot.service.ReserveService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/reserve")
public class ReserveController {

    @Resource
    ReserveService reserveService;

    // 创建预约
    @PostMapping("/create")
    public Result create(@RequestBody Reserve reserve) {
        return reserveService.createReserve(reserve);
    }

    // 更新预约
    @PutMapping("/update")
    public Result update(@RequestBody Reserve reserve) {
        reserveService.updateById(reserve);
        return Result.success();
    }

    // 删除预约
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        reserveService.removeById(id);
        return Result.success();
    }

    // 批量删除预约
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        reserveService.removeBatchByIds(ids);
        return Result.success();
    }

    // 处理预约：通过或者拒绝，并设置处理人和处理时间
    @PutMapping("/handle/{id}")
    public Result handle(@PathVariable Integer id, @RequestBody Map<String, Object> params) {
        return reserveService.handleReserve(id, params);
    }

    // 查询通过预约的学员
    @GetMapping("/selectApprove")
    public Result selectApprove(@RequestParam("username") String username) {
        List<Reserve> reserveList = reserveService.getReservesByUsernameAndState(username, "已通过");
        return Result.success(reserveList);
    }

    // 查询通过处理的人
    @GetMapping("/selectHandler")
    public Result selectByHandler(@RequestParam("username") String username) {
        List<Reserve> reserveList = reserveService.getReservesByHandlerAndState(username, "已通过");
        return Result.success(reserveList);
    }

    // 查询预约详情
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Reserve reserve = reserveService.getById(id);
        return Result.success(reserve);
    }

    // 分页多条件查询
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,
                               @RequestParam String content,
                               @RequestParam String type,
                               @RequestParam String username,
                               @RequestParam String createTime,
                               @RequestParam String reserveTime,
                               @RequestParam String remark,
                               @RequestParam String state) {
        Page<Reserve> page = reserveService.getReservesByPage(
                pageNum, pageSize, content, type, state, username, createTime, reserveTime, remark);
        return Result.success(page);
    }

    // 导出数据
    @AuthAccess
    @GetMapping("/export")
    public void exportData(@RequestParam(required = false) String username,
                           @RequestParam(required = false) String content,
                           @RequestParam(required = false) String ids,
                           HttpServletResponse response) throws IOException {
        reserveService.exportData(username, content, ids, response);
    }

    // 导入数据到数据库
    @AuthAccess
    @PostMapping("/import")
    public Result importData(@RequestParam("file") MultipartFile file) {
        try {
            return reserveService.importData(file);
        } catch (IOException e) {
            return Result.error("导入失败");
        }
    }

    // 统计当前学生预约数量
    @GetMapping("/countCurrentStudent")
    public Map<String, Integer> countCurrentStudent() {
        return reserveService.countCurrentStudent();
    }
}
